<template>
  <div class="layout-container">
    <div class="layout-container-form flex space-between">
      <div class="layout-container-form-handle">
        <el-button type="primary" :icon="Plus" @click="handleAdd">{{
          $t("message.common.add")
          }}
        </el-button>
      </div>
    </div>
    <div class="layout-container-table">
      <Table ref="table" v-loading="loading" :showPage="false" :data="tableData" highlight-current-row
             @getTableData="getTableData" @current-change="currentChange" @selection-change="handleSelectionChange">
        <el-table-column prop="name" label="角色名称" align="center"/>
        <el-table-column prop="description" label="角色描述" align="center"/>
        <el-table-column prop="status" label="状态" align="center" width="110">
          <template #default="scope">
            <span class="statusName">{{
                scope.row.status ? "启用" : "禁用"
            }}</span>
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
                       :loading="scope.row.loading" @click="handleUpdateStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column :label="$t('message.common.handle')" align="center" fixed="right" width="260">
          <template #default="scope">
            <el-button type="success" @click="handleResource(scope.row)">接口授权</el-button>
            <el-button :icon="Edit" @click="handleEdit(scope.row)"></el-button>
            <el-popconfirm :title="$t('message.common.delTip')" @confirm="handleDel([scope.row])">
              <template #reference>
                <el-button :icon="Delete" type="danger"></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </Table>
      <Layer :layer="layer" @getTableData="getTableData" v-if="layer.show"/>
      <LayerTree :layer="layertree" v-if="layertree.show"/>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive, inject} from "vue";
import {getData, del, update} from "@/api/system/role";
import {LayerInterface} from "@/components/layer/index.vue";
import {ElMessage} from "element-plus";
import Table from "@/components/table/index.vue";
import Layer from "./layer.vue";
import LayerTree from "./layer-tree.vue";
import {Plus, Search, Delete, Edit} from "@element-plus/icons";
// 存储搜索用的数据
const query = reactive({
  name: null,
});
// 弹窗控制器
const layer: LayerInterface = reactive({
  show: false,
  title: "新增",
  showButton: true,
});

// 弹窗控制器
const layertree: LayerInterface = reactive({
  show: false,
  title: "接口授权",
  width: "900px",
  showButton: false,
});

// 选中数据传输
const active: any = inject("active");

const loading = ref(true);
const tableData = ref([]);
const chooseData = ref([]);
const handleSelectionChange = (val: []) => {
  chooseData.value = val;
};

const currentChange = (currentRow: any, oldCurrentRow: any) => {
  if (currentRow) {
    let {roleId} = currentRow;
    active.value = roleId;
  }
};
// 获取表格数据
// params <init> Boolean ，默认为false，用于判断是否需要初始化分页
const getTableData = (init: Boolean) => {
  loading.value = true;
  getData(query)
      .then((res) => {
        let data = res.data;
        data.forEach((d: any) => {
          d.loading = false;
        });
        tableData.value = data;
      })
      .catch((error) => {
        tableData.value = [];
      })
      .finally(() => {
        loading.value = false;
      });
};
// 删除功能
const handleDel = (data: object[]) => {
  let params = {
    ids: data
        .map((e: any) => {
          return e.roleId;
        })
        .join(","),
  };
  del(params.ids).then((res) => {
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    getTableData(tableData.value.length === 1 ? true : false);
  });
};
// 新增弹窗功能
const handleAdd = () => {
  layer.title = "新增数据";
  layer.show = true;
  delete layer.row;
};
// 编辑弹窗功能
const handleEdit = (row: any) => {
  layer.title = "编辑数据";
  layer.row = row;
  layer.show = true;
};

// 接口授权弹窗功能
const handleResource = (row: any) => {
  layertree.title = "接口授权";
  layertree.row = row;
  layertree.show = true;
};

// 状态编辑功能
const handleUpdateStatus = (row: any) => {
  if (!row.roleId) {
    return;
  }
  row.loading = true;
  let params = {
    roleId: row.roleId,
    status: row.status,
  };

  update(params)
      .then((res) => {
        ElMessage({
          type: "success",
          message: "状态变更成功",
        });
      })
      .catch((err) => {
        ElMessage({
          type: "error",
          message: "状态变更失败",
        });
      })
      .finally(() => {
        row.loading = false;
      });
};

getTableData(false);
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100%;
  margin: 0 10px 0 0;
  width: calc(100% - 10px);
}
</style>
